package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.work.WorkManager
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.NameResultBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.NotificationUtil
import com.horoscope.astrology.zodiac.signs.free.utils.ShareManager
import kotlinx.android.synthetic.main.activity_name_result.*
import kotlinx.android.synthetic.main.item_share.*
import kotlinx.android.synthetic.main.view_network_error.*

class NameResultActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_name_result)
        initView()
        initData()
        activityShowAd("Interstitial_Name")
        AppSettingManager.saveInt(1, "name_result") // 成功使用了姓名匹配
    }

    private fun initData() {
        val name1 = intent.getStringExtra("name1") ?: ""
        val name2 = intent.getStringExtra("name2") ?: ""
        if (name1.isNotEmpty() && name2.isNotEmpty()) {
            request(name1, name2)
        } else {
            include_error.visibility = View.VISIBLE
        }
    }

    private fun initView() {
        initTitleBar(
            this,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            ""
        )
        // 文字阴影(发光效果)
//        tv_result_num?.setShadowLayer(16f, 0f, 0f, Color.WHITE)
        tv_network_ok.setOnClickListener {
            include_error.visibility = View.GONE
            initData()
        }
        ShareManager.initShare(
            this@NameResultActivity,
            null,
            null,
            null,
            image_view_more_share,
            tv_facebook
        )
    }

    private fun request(name: String, name2: String) {
        name_result_root?.visibility = View.INVISIBLE
        showLoading()
        NetworkEngine.getNameResult(name, name2, object : NetworkListener<NameResultBean>() {
            override fun onSuccess(t: NameResultBean) {
                tv_content?.text = t.data.content
                tv_result_num?.text = t.data.rate
                name_result_root?.visibility = View.VISIBLE
                showLoaded()
                include_error.visibility = View.GONE
                try {
                    if (intent.getBooleanExtra("Alarm", false)) { // 结束任务
                        WorkManager.getInstance(this@NameResultActivity)
                            .cancelUniqueWork(intent.getStringExtra("workName"))
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            override fun onFailed(t: NameResultBean) {
                super.onFailed(t)
                errotMake()
            }

            override fun onError(e: String?) {
                super.onError(e)
                errotMake()
            }

        })
    }

    private fun errotMake() {
        showLoaded()
        include_error.visibility = View.VISIBLE
    }

    companion object {
        fun generateIntent(
            context: Context,
            name: String,
            name2: String,
            alarm: Boolean = false,
            workName: String = ""
        ): Intent {
            val intent = Intent(context, NameResultActivity::class.java)
            intent.putExtra("Alarm", alarm)
            intent.putExtra("workName", workName)
            intent.putExtra("name1", name)
            intent.putExtra("name2", name2)
            return intent
        }
    }
}
